﻿//删除图书信息
void DeleteBook()
{
	FILE* pfbook;
	int BookID;
	int BookRecord;
	char Flag;

	system("cls");
	BookID = SearchBook();
	cout << endl;
	//文件打开失败
	if (BookID == -1)
	{
		return;
	}
	if (BookID == -3)
	{
		return;
	}

	BookRecord = ReadBookFile("rb");
	cout << "\t已经找到该图书，是否删除(y/n)：";
	cin >> Flag;
	cout << endl;

	int index = astBook[BookID].Num;//删除的信息编号

	//把要删除的记录后的数组全部向前移动一位，然后再写入文件中
	if (Flag == 'n')
	{
		return;
	}
	else if (Flag == 'y')
	{
		for (int i = BookID; i < BookRecord - 1; i++)
		{
			astBook[i] = astBook[i + 1];
		}
		BookRecord--;
	}

	//写入文件
	if (Flag == 'y')
	{
		pfbook = fopen("book.txt", "wb");
		if (pfbook != NULL)
		{
			for (int i = 0; i < BookRecord; i++)
			{
				if (fwrite(&astBook[i], LEN_BOOK, 1, pfbook) != 1)
				{
					cout << "\t无法保存该信息" << endl;
					return;
				}
			}
			fclose(pfbook);
			cout << "\t" << index << "号图书信息已经成功删除" << endl;
		}
	}
	else
	{
		cout << "\t输入错误，请退出重新输入" << endl;
	}
}

//保存到文件中
void SaveBookFile(int BookID)
{
	FILE* pfbook;
	pfbook = fopen("book.txt", "rb+");
	if (pfbook != NULL)
	{
		fseek(pfbook, LEN_BOOK * BookID, SEEK_SET);
		if (fwrite(&astBook[BookID], LEN_BOOK, 1, pfbook) != 1)
		{
			cout << "\t无法保存该信息";
		}
	}
	fclose(pfbook);
}
